/**
 * 级联选择器数据管理组合函数
 */
import { ref, readonly } from 'vue'
import type { CascaderOption } from '../types/app'

export function useCascaderData() {
  // 级联选择器数据
  const cascaderOptions = ref<CascaderOption[]>([
    {
      name: 'component',
      nameCn: '组件',
      nameEn: 'Component',
      children: [
        {
          name: 'basic',
          nameCn: '基础组件',
          nameEn: 'Basic',
          children: [
            { name: 'button', nameCn: '按钮', nameEn: 'Button' },
            { name: 'input', nameCn: '输入框', nameEn: 'Input' },
            { name: 'icon', nameCn: '图标', nameEn: 'Icon' },
            { name: 'image', nameCn: '图片', nameEn: 'Image' },
            { name: 'layout', nameCn: '布局', nameEn: 'Layout' }
          ]
        },
        {
          name: 'form',
          nameCn: '表单组件',
          nameEn: 'Form',
          children: [
            { name: 'select', nameCn: '选择器', nameEn: 'Select' },
            { name: 'cascader', nameCn: '级联选择器', nameEn: 'Cascader' },
            { name: 'switch', nameCn: '开关', nameEn: 'Switch' },
            { name: 'radio', nameCn: '单选框', nameEn: 'Radio' },
            { name: 'checkbox', nameCn: '复选框', nameEn: 'Checkbox' },
            { name: 'slider', nameCn: '滑块', nameEn: 'Slider' },
            { name: 'rate', nameCn: '评分', nameEn: 'Rate' },
            { name: 'upload', nameCn: '上传', nameEn: 'Upload' }
          ]
        },
        {
          name: 'data',
          nameCn: '数据展示',
          nameEn: 'Data',
          children: [
            { name: 'table', nameCn: '表格', nameEn: 'Table' },
            { name: 'pagination', nameCn: '分页', nameEn: 'Pagination' },
            { name: 'tree', nameCn: '树形控件', nameEn: 'Tree' },
            { name: 'list', nameCn: '列表', nameEn: 'List' },
            { name: 'card', nameCn: '卡片', nameEn: 'Card' },
            { name: 'timeline', nameCn: '时间线', nameEn: 'Timeline' }
          ]
        },
        {
          name: 'feedback',
          nameCn: '反馈组件',
          nameEn: 'Feedback',
          children: [
            { name: 'alert', nameCn: '警告提示', nameEn: 'Alert' },
            { name: 'message', nameCn: '消息提示', nameEn: 'Message' },
            { name: 'notification', nameCn: '通知提醒', nameEn: 'Notification' },
            { name: 'modal', nameCn: '对话框', nameEn: 'Modal' },
            { name: 'drawer', nameCn: '抽屉', nameEn: 'Drawer' },
            { name: 'popover', nameCn: '气泡卡片', nameEn: 'Popover' }
          ]
        },
        {
          name: 'navigation',
          nameCn: '导航组件',
          nameEn: 'Navigation',
          children: [
            { name: 'menu', nameCn: '导航菜单', nameEn: 'Menu' },
            { name: 'breadcrumb', nameCn: '面包屑', nameEn: 'Breadcrumb' },
            { name: 'tabs', nameCn: '标签页', nameEn: 'Tabs' },
            { name: 'steps', nameCn: '步骤条', nameEn: 'Steps' },
            { name: 'dropdown', nameCn: '下拉菜单', nameEn: 'Dropdown' }
          ]
        }
      ]
    },
    {
      name: 'guide',
      nameCn: '指南',
      nameEn: 'Guide',
      children: [
        {
          name: 'design',
          nameCn: '设计原则',
          nameEn: 'Design Principles',
          children: [
            { name: 'consistency', nameCn: '一致性', nameEn: 'Consistency' },
            { name: 'feedback', nameCn: '反馈', nameEn: 'Feedback' },
            { name: 'efficiency', nameCn: '效率', nameEn: 'Efficiency' },
            { name: 'controllability', nameCn: '可控性', nameEn: 'Controllability' }
          ]
        },
        {
          name: 'development',
          nameCn: '开发指南',
          nameEn: 'Development Guide',
          children: [
            { name: 'installation', nameCn: '安装', nameEn: 'Installation' },
            { name: 'quickstart', nameCn: '快速开始', nameEn: 'Quick Start' },
            { name: 'customization', nameCn: '定制化', nameEn: 'Customization' },
            { name: 'theming', nameCn: '主题配置', nameEn: 'Theming' }
          ]
        }
      ]
    },
    {
      name: 'resources',
      nameCn: '资源',
      nameEn: 'Resources',
      children: [
        {
          name: 'design-resources',
          nameCn: '设计资源',
          nameEn: 'Design Resources',
          children: [
            { name: 'sketch', nameCn: 'Sketch 模板', nameEn: 'Sketch Templates' },
            { name: 'figma', nameCn: 'Figma 组件库', nameEn: 'Figma Components' },
            { name: 'axure', nameCn: 'Axure 组件库', nameEn: 'Axure Components' }
          ]
        },
        {
          name: 'development-tools',
          nameCn: '开发工具',
          nameEn: 'Development Tools',
          children: [
            { name: 'cli', nameCn: '命令行工具', nameEn: 'CLI Tools' },
            { name: 'vscode', nameCn: 'VS Code 插件', nameEn: 'VS Code Extensions' },
            { name: 'webpack', nameCn: 'Webpack 插件', nameEn: 'Webpack Plugins' }
          ]
        }
      ]
    }
  ])

  /**
   * 根据路径查找选项
   */
  const findOptionByPath = (path: string[]): CascaderOption | null => {
    let current = cascaderOptions.value
    let result: CascaderOption | null = null

    for (const name of path) {
      const found = current.find(option => option.name === name)
      if (!found) {
        return null
      }
      result = found
      current = found.children || []
    }

    return result
  }

  /**
   * 获取选项的完整路径标签
   */
  const getOptionLabels = (path: string[]): string[] => {
    const labels: string[] = []
    let current = cascaderOptions.value

    for (const name of path) {
      const found = current.find(option => option.name === name)
      if (!found) {
        break
      }
      labels.push(found.nameCn)
      current = found.children || []
    }

    return labels
  }

  /**
   * 检查路径是否有子选项
   */
  const hasChildren = (path: string[]): boolean => {
    const option = findOptionByPath(path)
    return !!(option?.children?.length)
  }

  /**
   * 获取所有叶子节点
   */
  const getLeafOptions = (): CascaderOption[] => {
    const leaves: CascaderOption[] = []

    const traverse = (options: CascaderOption[]) => {
      for (const option of options) {
        if (!option.children || option.children.length === 0) {
          leaves.push(option)
        } else {
          traverse(option.children)
        }
      }
    }

    traverse(cascaderOptions.value)
    return leaves
  }

  /**
   * 搜索选项（根据中文或英文名称）
   */
  const searchOptions = (keyword: string): CascaderOption[] => {
    const results: CascaderOption[] = []
    const lowerKeyword = keyword.toLowerCase()

    const traverse = (options: CascaderOption[]) => {
      for (const option of options) {
        if (
          option.nameCn.toLowerCase().includes(lowerKeyword) ||
          option.nameEn.toLowerCase().includes(lowerKeyword) ||
          option.name.toLowerCase().includes(lowerKeyword)
        ) {
          results.push(option)
        }

        if (option.children) {
          traverse(option.children)
        }
      }
    }

    traverse(cascaderOptions.value)
    return results
  }

  return {
    // 只读的级联选择器数据
    cascaderOptions: readonly(cascaderOptions),
    
    // 工具方法
    findOptionByPath,
    getOptionLabels,
    hasChildren,
    getLeafOptions,
    searchOptions
  }
}
